package com.gla.market.fragments;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.gla.market.R;
import com.gla.market.activity.Shop_FYHActivity;


import com.gla.market.activity.Video_FYHActivity;
import com.gla.market.adapter.AfterSearchAdapter;
import com.gla.market.adapter.FYH_CommodityAdapter;



import com.gla.market.adapter.VideoContentAdapter;
import com.gla.market.constant.Constants;
import com.gla.market.entity.Video;
import com.gla.market.utils.NetUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * 视频页面
 */

public class video extends Fragment {

    private Context mContext;
    private ListView video_item_list;  //适配器名字

    List<Video> videoItemList = new ArrayList<>();

    private VideoContentAdapter videoContentAdapter;  //适配器对象


    //用于从网络初始化UI的handler
    private Handler uiHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case -1:
                    //网络请求失败
                    Toast.makeText(mContext, "网络请求失败！", Toast.LENGTH_SHORT).show();
                    break;
                case 0:
                    //网络请求成功，但是返回状态为失败
                    Toast.makeText(mContext, msg.obj == null ? "获取商店数据失败！" : msg.obj.toString(), Toast.LENGTH_SHORT).show();
                    break;
                case 2:
                    //监听适配器列表
                    video_item_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Video videoItem = videoItemList.get(position);
                            Integer video_id = videoItem.getId();
                            Intent intent = new Intent(mContext, Video_FYHActivity.class);
                            intent.putExtra("id",video_id + "");
                            startActivity(intent);
                        }
                    });
                    //成功调用，到adapter赋值
                    videoContentAdapter = new VideoContentAdapter(mContext,videoItemList);
                    video_item_list.setAdapter(videoContentAdapter);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContext = getContext();
        View view = inflater.inflate(R.layout.fragment_video, null);
        //初始化控件
        initView(view);
        super.onCreate(savedInstanceState);

        initAdapter(view);
        initDataAdater(view,"all"); //适配器数据初始化

        return view;
    }


    //初始化适配器
    private void initAdapter(View view){
        video_item_list = view.findViewById(R.id.video_content_shop_item);
    }

    //初始化适配器的内容
    private void initDataAdater(View view,String type){

        if(type == "all"){
            //调用后端，获取json数据
            String url = Constants.BASE_URL + Constants.GET_VIDEO_CONTENT_URL + Constants.GET_ALL_SHOP;
            NetUtil.doGet(url, new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            Log.e("调试", "错误信息：" + e.toString());
                            Message message = Message.obtain();
                            message.what = -1;
                            uiHandler.sendMessage(message);
                        }
                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            String result = response.body().string().trim();
                            com.alibaba.fastjson.JSONObject object = JSON.parseObject(result);
                            Message message = Message.obtain();
                            System.out.println(result);
                            if (object.getString("error")==null && object.getInteger("code")==100) {
                                JSONArray array = object.getJSONArray("data");
                                //获得商品list
                                System.out.println("或取商品成功");
                                videoItemList = JSONArray.parseArray(array.toString(), Video.class);
                                System.out.println(videoItemList);
                                System.out.println("赋值商品成功");
                                message.what = 2;
                            } else {
                                message.what = 0;
                            }
                            uiHandler.sendMessage(message);
                        }
                    }
            );
        }
    }


    //初始化控件
    private void initView(View view){


    }


}
